using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library.Events;
using NCS.Library;
using NCS.Library.Components.Security;
using NCS.Library.Components;

namespace NCS.Module.CMS.Admin.WorkFlow
{
    public partial class EventDetail : AuthenticatedControl
    {
        private EventInfo eventInfo;
        private int total;
        protected void Page_Load(object sender, EventArgs e)
        {
            btnRemoveCheck.Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
            if (Request.QueryString["eventid"] != null)
            {
                eventInfo = EventController.GetInfo(ConvertUtility.ToInt32(Request.QueryString["eventid"]));
                if (eventInfo == null) { this.Visible = false; return; }
                else
                {
                    lnkEvent.Text = eventInfo.Event_Name; lnkEvent.ToolTip = eventInfo.Event_Description;
                    //lnkEvent.NavigateUrl = AppEnv.AdminUrlParams("cmseventlist");
                    lnkEvent.NavigateUrl = AppEnv.AdminUrlParams("eventlist");
                }
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            string key = "NCS.Portal.CMS.Event";
            DataCaching data = new DataCaching();
            data.RemoveCache(key);

          //  DataTable source = EventController.Event_GetNewsByEventID(eventInfo.Event_ID, 0);
            DataTable source = EventController.GetNewsByEventID(eventInfo.Event_ID);
            total = source.Rows.Count;
            dgEventDetail.DataSource = source;
            dgEventDetail.DataBind();
        }       

        protected void dgEventDetail_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView curData = (DataRowView)e.Item.DataItem;
                HyperLink hplheadline = (HyperLink)e.Item.FindControl("hplheadline");
                hplheadline.Text = curData["Content_Headline"].ToString();
                //hplheadline.NavigateUrl = AppEnv.AdminUrlParams("cms_viewcontent") + "&contentid=" + curData["Distribution_ContentID"].ToString();
                hplheadline.NavigateUrl = AppEnv.AdminUrlParams("cms_viewcontent") + "&contentid=" + curData["Content_ID"].ToString();
                e.Item.Cells[e.Item.Cells.Count - 1].Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
                DropDownList dropPriority = (DropDownList)e.Item.FindControl("dropPriority");
                MiscUtility.FillIndex(dropPriority, 0, total, (int)curData["Priority"]);
            }
        }

        protected void btnAddDistribution_Click(object sender, EventArgs e)
        {
            Response.Redirect(AppEnv.AdminUrlParams("cms_distributedlist") + "&eventid=" + eventInfo.Event_ID);
        }

        protected void btnUpdatePriority_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataGridItem item in dgEventDetail.Items)
                {
                    DropDownList dropPriority = (DropDownList)item.FindControl("dropPriority");
                    EventController.Event_SetDistributionPriority(eventInfo.Event_ID, Convert.ToInt32(item.Cells[0].Text), Convert.ToInt32(dropPriority.SelectedValue));
                    this.AddLog("CMS Event detail", " try set distribtion priority of event " + eventInfo.Event_Name + " to " + dropPriority.SelectedValue);
                }
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch(Exception ex)
            {
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_ERROR;
                this.AddLog("CMS Event detail", " try update event priority error: " + ex.Message);
            }
        }

        protected void btnRemoveCheck_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataGridItem item in dgEventDetail.Items)
                {
                    CheckBox chkCheck = (CheckBox)item.FindControl("chkCheck");
                    if (chkCheck.Checked) EventController.Event_RemoveDistribution(eventInfo.Event_ID, Convert.ToInt32(item.Cells[0].Text));
                    this.AddLog("CMS Event detail", " try remove distribution " + item.Cells[0].Text + " from event " + eventInfo.Event_Name + " success");
                }
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch(Exception ex)
            {
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_ERROR;
                this.AddLog("CMS Event detail", " try remove distribution from event error: " + ex.Message);
            }
        }

        protected void dgEventDetail_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                try
                {
                    EventController.Event_RemoveDistribution(eventInfo.Event_ID, Convert.ToInt32(e.Item.Cells[0].Text));
                    lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                    this.AddLog("CMS Event detail", " try remove distribution " + e.Item.Cells[0].Text + " from event error: " + eventInfo.Event_Name);
                }
                catch(Exception ex)
                {
                    lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_ERROR;
                    this.AddLog("CMS Event detail", " try remove distribution from event error: " + ex.Message);
                }
            }
        }
    }
}